import NodeUtil from "./NodeUtil";

/**
 * tiledMap 坐标换算工具类
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class TiledMapUtils {
    
  public static openglToTile(tiledMap:cc.TiledMap,point: cc.Vec2) {
    let mapSize = tiledMap.getMapSize();
    let tileSize = tiledMap.getTileSize();
    let y = Math.floor((mapSize.height - 2 - ((2 * Math.floor(point.y) / Math.floor(tileSize.height)))));
    let x = Math.floor(point.x / tileSize.width - (y ) / 2);
  
    return cc.v2(x, y);
  }   


    //OpenGL转Creator：
    public static openglToScreen(tiledMap:cc.TiledMap,point: cc.Vec2) {
      let mapSize = tiledMap.getMapSize();
      let tileSize =tiledMap.getTileSize();
      let x = point.x - mapSize.width * tileSize.width / 2;
      let y = point.y - mapSize.height * tileSize.height / 2;
      return cc.v2(x, y);
    }


      //Creator转OpenGL：
      public static screenToOpengl(tiledMap:cc.TiledMap,point: cc.Vec2) {
        let mapSize = tiledMap.getMapSize();
        let tileSize = tiledMap.getTileSize();
        let x = point.x + mapSize.width * tileSize.width / 2;
        let y = point.y + mapSize.height * tileSize.height / 2; 
        return cc.v2(x, y);
      }
 
      /**
       * 获取地图中45度的坐标
       * 对象的offset{x,y}
       * @param tiledMap 
       * @param point 
       * @returns 
       */
     static getTitledPos(tiledMap:cc.TiledMap,point:cc.Vec2){
        let {width,height} = tiledMap.getTileSize(); 
        var cellX = point.x / (width / 2);
        var cellY = point.y / height;
        return cc.v2(cellX,cellY)
      }

      //通过地图中的坐标 获取openGl坐标
      static tiledToOpengl(tiledMap:cc.TiledMap,cell:cc.Vec2){
        let mapSize = tiledMap.getMapSize(); 
        let cellWidth = tiledMap.getTileSize().width; 
        let cellHeight = tiledMap.getTileSize().height; 
        let mapPixWidth = cellWidth * mapSize.width;
        let mapPixHeight = cellHeight * mapSize.height;
        let posX = mapPixWidth / 2 + (cell.x - cell.y) * cellWidth / 2;
        let posY = mapPixHeight - (cell.x +  cell.y) * cellHeight / 2;
        return cc.v2(posX,posY)
      } 

      //世界坐标转地图坐标
     static  worldPos2MapPos(tiledMap:cc.TiledMap,v2:cc.Vec2){ 
        let tiledMapV2 = NodeUtil.worldConvertLocalPointAR(tiledMap.node,v2) 
        let openGlV2 = TiledMapUtils.screenToOpengl(tiledMap,tiledMapV2)
        let x = openGlV2.x
        let y = openGlV2.y 
        // let obj = tiledMap.getObjectGroup("build").getObject("obj1");
        let obj = tiledMap.getLayer("tile").getPositionAt(0,0)
        let tiledSize = tiledMap.getTileSize();
        let w = tiledSize.width/2;
        let h = tiledSize.height/2;
        let W = obj.x
        let H = obj.y
        let m:number = Number(((H*w - y*w + x*h - W*h) / (2*w*h)).toFixed(0))
        let n:number = Number(((H*w - y*w + W*h - x*h) / (2*w*h) + 1).toFixed(0)) 
        return {m:m,n:n};
      }
 
    /**
     * 将触点转换成tileMap地图方块坐标
     * @param tileMap cc.tiledMap 组件
     * @param x 触点转换为tileMap节点坐标系下的x坐标
     * @param y 触点转换为tileMap节点坐标系下的y坐标
     * @returns {x:number,y:number} 地图方块坐标
     */
    public static mathPointToMap(tileMap: cc.TiledMap, x: number, y: number): { x: number, y: number } {
      let tilesize = tileMap.getTileSize();
      let halfW = tilesize.width / 2;
      let haflH = tilesize.height / 2;
      let xiebian = Math.sqrt(Math.pow(halfW, 2) + Math.pow(haflH, 2));
      let cos = halfW / xiebian * xiebian;
      let sin = haflH / xiebian * xiebian; 
      let mapx: number, mapy: number;
      //节点锚点x到tileMap地图锚点x的偏移值/2
      let xoffset = tileMap.node.getContentSize().width * (0.5 - tileMap.node.getAnchorPoint().x);
      //节点锚点x到tileMap地图锚点y的偏移值/2
      let yoffset = tileMap.node.getContentSize().height * (1 - tileMap.node.getAnchorPoint().y);
      let diffx = x - xoffset;
      let diffy = -(y - yoffset);
      let { x: mx, y: my } = { x: 0, y: 0 }
      mapx = (1 / 2) * (diffx / cos + diffy / sin) + mx;
      mapy = (1 / 2) * (diffy / sin - diffx / cos) + my;
      return { x: Math.floor(mapx), y: Math.floor(mapy) }; 
  }

    /**
     * 将砖块坐标转换成节点坐标
     * @param tilePos tile方块在父节点坐标系的坐标，以（0，0）锚点为计算
     * @param tileMap 父节点
     * @returns 
     */
    public static tileToNode(tileMap: cc.TiledMap, tilePos: cc.Vec2) { 
        let x: number, y: number; 
        //父节点宽高的一半
        let halfW = tileMap.node.width / 2;
        let halfH = tileMap.node.height / 2;
        //算出以锚点0.5的坐标偏移值
        let offsetX = tilePos.x - halfW;
        let offsetY = tilePos.y - halfH;
        //得出最终坐标
        x = offsetX >= 0 ? Math.abs(offsetX) : -Math.abs(offsetX);
        y = offsetY >= 0 ? Math.abs(offsetY) : -Math.abs(offsetY); 
        let tileSize = tileMap.getTileSize();
        x += tileSize.width / 2;
        y += tileSize.height / 2; 
        return new cc.Vec2(x, y);
    }
     
      

}
